package com.zhonghui.controller;

import com.zhonghui.common.core.result.FwResult;
import com.zhonghui.model.UserAddress;
import com.zhonghui.service.UserAddressService;
import com.zhonghui.service.config.AuthUser;
import com.zhonghui.vo.OrderCreateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/address")
@RestController
public class UserAddressController {

    @Autowired
    private UserAddressService userAddressService;

    @PostMapping
    public FwResult list(){
        List<UserAddress> list = userAddressService.lambdaQuery()
                .eq(UserAddress::getCreateUser, AuthUser.getUserId()).list();
        return FwResult.ok(list);
    }

    @PostMapping("/add")
    public FwResult add(@RequestBody UserAddress userAddress){
        userAddress.setCreateUser(String.valueOf(AuthUser.getUserId()));
        userAddress.setUpdateUser(String.valueOf(AuthUser.getUserId()));
        userAddressService.save(userAddress);
        return FwResult.ok();
    }

    @PutMapping("{id}")
    public FwResult up(@PathVariable("id") long id, @RequestBody UserAddress userAddress){
        userAddress.setUpdateUser(String.valueOf(AuthUser.getUserId()));
        userAddress.setId(id);
        userAddressService.updateById(userAddress);
        return FwResult.ok();
    }

    @DeleteMapping("{id}")
    public FwResult del(@PathVariable("id") long id){
        userAddressService.removeById(id);
        return FwResult.ok();
    }

}
